# Create your views here.
from django.http import HttpResponse,HttpResponseRedirect
from polls.models import Poll,Choice
from django.template import Context, loader
from django.template import RequestContext
from django.http import Http404
from django.shortcuts import render_to_response,get_object_or_404,render
from django.core.urlresolvers import reverse
from polls.forms import ContactForm
from django.views.generic.edit import FormView
'''
def index(request):
    latest_poll_list = Poll.objects.all().order_by('pub_date')[:5];
    latest_choice_list = Choice.objects.all()[:];
    output = ','.join([p.question for p in latest_poll_list]);
    t = loader.get_template('index.html')
    c = Context({
               'latest_poll_list': latest_poll_list,
               'latest_choice_list':latest_choice_list,
              })
    return HttpResponse(t.render(c))
   # return HttpResponse("Hello,world.You're at the poll index");

def detail(request,poll_id):
    try:
        p = Poll.objects.get(pk=poll_id)
    except Poll.DoesNotExist:
        raise Http404
    p = get_object_or_404(Poll,pk=poll_id)
    return render_to_response('detail.html',{'poll':p},
                              context_instance=RequestContext(request));
    #return HttpResponse("You're looking at poll %s." % poll_id);

def results(request,poll_id):
    p = get_object_or_404(Poll,pk=poll_id);
    dic = {'poll':p}
    return render_to_response('results.html',dic);
   # return HttpResponse("You're looking at the results of poll %s." % poll_id);
'''
def vote(request,poll_id):
    p = get_object_or_404(Poll,pk=poll_id);
    try:
        selected_choice = p.choice_set.get(pk=request.POST['choice'])
    except(KeyError,Choice.DoesNotExist):
        return render_to_response('detail.html',{
            'poll':p,
            'error_message':"You didn't select a choice!"
            },context_instance=RequestContext(request))
    else:
        selected_choice.votes+=1;
        selected_choice.save();
    return HttpResponseRedirect(reverse('poll_results',args=(p.id,)));
   # return HttpResponse("You're voting on poll %s." % poll_id);
'''
def contact(request):
    if(request.method == 'POST'):
        form = ContactForm(request.POST)
        if(form.is_valid()):
            return HttpResponseRedirect("/polls/");
    else:
        form = ContactForm();

    return render(request,'contact.html',{
            'form':form,
        });
'''
def contactAction(request):
    return render(request,'old film/index.html')
